package response

import (
	"github.com/gin-gonic/gin"
)

type Response struct {
	Code HttpCode    `json:"code"`
	Msg  HttpMsg     `json:"msg"`
	Data interface{} `json:"data"`
}

func Success(ctx *gin.Context, data interface{}) {
	ctx.JSON(200, &Response{
		Code: successCode,
		Msg:  successMsg,
		Data: data,
	})
}

func Fail(ctx *gin.Context, code HttpCode) {
	var failMsg HttpMsg
	switch code {
	case notFoundCode:
		failMsg = notFoundMsg
	case errorCode:
		failMsg = errorMsg
	case notLoginCode:
		failMsg = notLoginMsg
	case noPermissionCode:
		failMsg = noPermissionMsg
	case badRequestCode:
		failMsg = badRequestMsg
	case LoginErrorCode:
		failMsg = loginErrorMsg
	case badRequestParamsCode:
		failMsg = badRequestParamsMsg
	default:
		failMsg = "非法状态"
		code = 000000000
	}
	ctx.JSON(200, &Response{
		Code: code,
		Msg:  failMsg,
		Data: nil,
	})
}
